import {
  deepClone
} from '@/utils/index'

const DRAWING_ITEMS = 'drawingItems'
const DRAWING_ITEMS_VERSION = '1.2'
const DRAWING_ITEMS_VERSION_KEY = 'DRAWING_ITEMS_VERSION'
const DRAWING_ID = 'idGlobal'
const TREE_NODE_ID = 'treeNodeId'
const FORM_CONF = 'formConf'

export function getDrawingList() {
  // 加入缓存版本的概念，保证缓存数据与程序匹配
  const version = localStorage.getItem(DRAWING_ITEMS_VERSION_KEY)
  if (version !== DRAWING_ITEMS_VERSION) {
    localStorage.setItem(DRAWING_ITEMS_VERSION_KEY, DRAWING_ITEMS_VERSION)
    saveDrawingList([])
    return null
  }

  const str = localStorage.getItem(DRAWING_ITEMS)

  if (str && str != 'undefined') return JSON.parse(str)
  return null
}

export function saveDrawingList(list) {
  localStorage.setItem(DRAWING_ITEMS, JSON.stringify(list))
}

export function clearDrawingList() {
  localStorage.setItem(DRAWING_ITEMS, '')
}

export function getIdGlobal() {
  const str = localStorage.getItem(DRAWING_ID)
  if (str) return parseInt(str, 10)
  return 100
}

export function saveIdGlobal(id) {
  localStorage.setItem(DRAWING_ID, `${id}`)
}

export function getTreeNodeId() {
  const str = localStorage.getItem(TREE_NODE_ID)
  if (str) return parseInt(str, 10)
  return 100
}

export function saveTreeNodeId(id) {
  localStorage.setItem(TREE_NODE_ID, `${id}`)
}

export function getFormConf() {
  const str = localStorage.getItem(FORM_CONF)
  if (str) return JSON.parse(str)
  return null
}

export function saveFormConf(obj) {
  localStorage.setItem(FORM_CONF, JSON.stringify(obj))
}

export function cleareFormConf() {
  localStorage.setItem(FORM_CONF, '')
}

/**
 * 根据表单ID从远程获取表单设计信息
 * @param {Object} id
 */
export function getFormConfToDB(id, success, fail) {
  const loading = ELEMENT.Loading.service({
    fullscreen: true,
    lock: true,
    text: '加载中...',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)'
  })

  this.$axios({
    method: 'get',
    url: `/wuyan/api/pub-form/${id}`
  }).then(res => {
    loading.close()

    if (res.fields) {
      res.fields = JSON.parse(res.fields)
    }

    if (success) success(res)
  }).catch(err => {
    loading.close()

    if (fail) fail(err)
  })
}

/**
 * 保存到数据库
 * @param {Object} obj
 */
export function saveFormConfToDB(obj, success, fail) {
  const loading = ELEMENT.Loading.service({
    fullscreen: true,
    lock: true,
    text: '保存中...',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)'
  })

  const param = deepClone(obj)
  param.fields = JSON.stringify(obj.fields)

  this.$axios({
    method: 'post',
    url: '/wuyan/api/pub-form',
    data: param
  }).then(res => {
    loading.close()

    if (success) success(res)
  }).catch(err => {
    loading.close()

    if (fail) fail(err)
  })
}

/**
 * 保存到数据库
 * @param {Object} obj
 */
export function updateFormConfToDB(id, obj, success, fail) {
  const loading = ELEMENT.Loading.service({
    fullscreen: true,
    lock: true,
    text: '保存中...',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)'
  })

  const param = deepClone(obj)
  param.fields = JSON.stringify(obj.fields)

  this.$axios({
      method: 'put',
      url: `/wuyan/api/pub-form/${id}`,
      data: param
    })
    .then(res => {
      loading.close()

      if (success) success(res)
    })
    .catch(err => {
      loading.close()

      if (fail) fail(err)
    })
}

/**
 * 根据表单名称和数据ID从远程获取数据明细
 */
export function getFormDataToDB(form, id, success, fail) {
  const loading = ELEMENT.Loading.service({
    fullscreen: true,
    lock: true,
    text: '加载中...',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)'
  })

  this.$axios({
    method: 'get',
    url: `/form/api/${form}/${id}`
  }).then(res => {
    loading.close()

    if (success) success(res)
  }).catch(err => {
    loading.close()

    if (fail) fail(err)
  })
}

/**
 * 将数据存储到指定表单
 */
export function saveFormDataToDB(form, obj, success, fail) {
  const loading = ELEMENT.Loading.service({
    fullscreen: true,
    lock: true,
    text: '保存中...',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)'
  })

  const param = deepClone(obj)

  this.$axios({
    method: 'post',
    url: `/form/api/${form}`,
    data: param
  }).then(res => {
    loading.close()

    if (success) success(res)
  }).catch(err => {
    loading.close()

    if (fail) fail(err)
  })
}

/**
 * 更新指定表单的指定ID的数据
 */
export function updateFormDataToDB(form, id, obj, success, fail) {
  const loading = ELEMENT.Loading.service({
    fullscreen: true,
    lock: true,
    text: '保存中...',
    spinner: 'el-icon-loading',
    background: 'rgba(255, 255, 255, 0.5)'
  })

  const param = deepClone(obj)

  this.$axios({
      method: 'put',
      url: `/form/api/${form}/${id}`,
      data: param
    })
    .then(res => {
      loading.close()

      if (success) success(res)
    })
    .catch(err => {
      loading.close()

      if (fail) fail(err)
    })
}
